/**
 * 
 */
package com.et114.components.ldap;

import javax.naming.NamingException;
import javax.naming.directory.Attributes;

import org.springframework.ldap.core.AttributesMapper;
import org.springframework.util.Assert;

import com.et114.core.exception.CheckException;
import com.et114.core.utility.ArgumentTokenizer;

/**
 * @author guanhw
 */
public class SimpleLdapUtils {

	public static AttributesMapper getAttributesMapper( final String am ) throws CheckException {
		return new AttributesMapper() {
			public Object mapFromAttributes ( Attributes attrs )
					throws NamingException {
				if ( am == null || "".equals( am ) ) return attrs.getAll();
				return attrs.get( am ).get();
			}
		}  ;
	}
	
	
	public static String getAttr( String str , String attr ) throws CheckException  {
		Assert.state( !( str == null  || "".equals( str.trim() )  ) , "name不能为空！" ) ; 
		Assert.state( !( attr == null  || "".equals( attr.trim() )  ) , "attr不能为空！" ) ; 
		ArgumentTokenizer at = new ArgumentTokenizer( str , ':' ) ; 
		while( at.hasNext() ) {
			String _name = at.next() ;
			if ( attr.equals(  _name  ) ) {
				return at.next() ;   
			}
		} 
		return null ;
	}
	/**
	 * 
	 */
	public SimpleLdapUtils ( ) {
		// TODO Auto-generated constructor stub
	}

}
